﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ABC.Google
{
    public static class GoogleDistance
    {

        public static double distFrom(double lat1, double lng1, double lat2, double lng2)
        {
            double earthRadius = 3958.75;
            double dLat = toRadians(lat2 - lat1);
            double dLng = toRadians(lng2 - lng1);
            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                       Math.Cos(toRadians(lat1)) * Math.Cos(toRadians(lat2)) *
                       Math.Sin(dLng / 2) * Math.Sin(dLng / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double dist = earthRadius * c;
            int meterConversion = 1609;
            double milesConversion = 0.000621371;
            return (dist * meterConversion) * milesConversion;
        }

        static double toRadians(double degree)
        {
            return (degree * 0.0174532925);
        }

    }
}
